﻿Imports System.Text
Imports Autodesk
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports Model
Imports Model.Geometry

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''      与CAD交互操作相关方法
'' 1、获取或创建cad文档
'' 2、在文档中绘制一个实体圆
'' 3、在文档中绘制一个实体点
'' 4、在文档中绘制一个标注文本
'' 5、在文档中绘制一条直线
'' 6、在文档中新增图层并设置图层样式
'' 7、在文档中添加标注并设置标注样式
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' <summary>
''' 与CAD交互操作相关方法类
''' </summary>
''' <remarks></remarks>
Public Class CADHelper
    ''' <summary>
    ''' 主程序
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Mycad As Object
    ''' <summary>
    ''' 当前cad文档
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared acaddoc As AutoCAD.Interop.AcadDocument
    ''' <summary>
    ''' 当前模型空间
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared acadmodelspace As Common.AcadModelSpace
    ''' <summary>
    ''' 点图层
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared acadpointlayer As AcadLayer
    ''' <summary>
    ''' 圆弧图层，拟合图层
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared acadcirclelayer As AcadLayer
    ''' <summary>
    ''' 线图层
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared acadlinelayer As AcadLayer
    ''' <summary>
    ''' 标注图层
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared acadtextlayer As AcadLayer
    ''' <summary>
    ''' 字体样式
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared textstyle As AcadTextStyle

    Public Shared Mypath As String

    ''' <summary>
    ''' 获取或创建cad文档
    ''' </summary>
    ''' <returns>返回cad程序对象</returns>
    ''' <remarks></remarks>
    Public Shared Function GetAcadApplication(ByVal ppath As String)
        ''在下面添加获取或创建cad文档的代码
        Dim acad As AutoCAD.Interop.AcadApplication
        On Error Resume Next
        acad = GetObject(, "AutoCAD.Application")
        If Err.Number Then
            Err.Clear()
            acad = CreateObject("AutoCAD.Application.16.2")
            If Err.Number Then
                MsgBox(Err.Description)
                Return Nothing
            End If
        End If
        acad.Visible = True
        Mycad = acad
        acaddoc = acad.ActiveDocument
        acadmodelspace = acaddoc.ModelSpace
        Mypath = ppath
        Return Nothing
    End Function

    ''' <summary>
    ''' 在cad文档中绘制一个圆
    ''' </summary>
    ''' <param name="center">圆心，点</param>
    ''' <param name="Radius">半径</param>
    ''' <returns>返回cad圆对象</returns>
    ''' <remarks></remarks>
    Public Shared Function DrawCircle(ByVal center As Point3d, ByVal Radius As Double)
        ''在下面添加绘制一个圆的代码
        ''激活绘制圆的图层
        acaddoc.ActiveLayer = acadcirclelayer
        Dim cen(0 To 2) As Double
        cen(0) = center.p_x
        cen(1) = center.p_y
        cen(2) = center.p_z
        acadmodelspace.AddCircle(cen, Radius)
        Return Nothing
    End Function

    Public Shared Function DrawArc(ByVal center As Point3d, ByVal Radius As Double, ByVal startAngle As Double, ByVal endAngle As Double)
        ''在下面添加一个绘制圆弧的代码
        acaddoc.ActiveLayer = acadcirclelayer
        Dim cen(0 To 2) As Double
        cen(0) = center.p_x
        cen(1) = center.p_y
        cen(2) = center.p_z
        acadmodelspace.AddArc(cen, Radius, startAngle, endAngle)
        Return Nothing
    End Function

    ''' <summary>
    ''' 马蹄形隧道拟合结果绘制
    ''' </summary>
    ''' <param name="center"></param>
    ''' <param name="Radius"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DrawHorse(ByVal center As Point3d, ByVal Radius As Double)

        Return Nothing
    End Function

    ''' <summary>
    ''' 在cad文档中绘制一个点
    ''' </summary>
    ''' <param name="pt">实体点</param>
    ''' <returns>返回点对象</returns>
    ''' <remarks></remarks>
    Public Shared Function DrawPoint(ByVal pt As Point3d)
        ''在下面添加绘制一个点的代码
        ''激活绘制点的图层
        acaddoc.ActiveLayer = acadpointlayer
        Dim p(0 To 2) As Double
        p(0) = pt.p_x
        p(1) = pt.p_y
        p(2) = pt.p_z
        acadmodelspace.AddPoint(p)
        Return Nothing
    End Function

    ''' <summary>
    ''' 在cad文档中绘制一条直线
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DrawLine(ByVal spt As Point3d, ByVal ept As Point3d)
        ''在下面添加绘制一条直线的代码
        ''激活绘制直线的图层
        acaddoc.ActiveLayer = acadlinelayer
        Dim s(0 To 2), e(0 To 2) As Double
        s(0) = spt.p_x
        s(1) = spt.p_y
        s(2) = spt.p_z
        e(0) = ept.p_x
        e(1) = ept.p_y
        e(2) = ept.p_z
        acadmodelspace.AddLine(s, e)
        Return Nothing
    End Function

    ''' <summary>
    ''' 在当前文档中添加图层
    ''' </summary>
    ''' <param name="layername"></param>
    ''' <param name="colorname"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function AddLayer(ByVal layername As String, ByVal colorname As AcColor, ByRef layerid As AcadLayer)
        layerid = acaddoc.Layers.Add(layername)
        layerid.color = colorname
        Return Nothing
    End Function

    ''' <summary>
    ''' 在当前文档中添加文字
    ''' </summary>
    ''' <param name="pt"></param>
    ''' <param name="stringtext"></param>
    ''' <param name="h"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function AddText(ByVal pt As Point3d, ByVal stringtext As String, ByVal h As Double)
        acaddoc.ActiveTextStyle = textstyle
        acaddoc.ActiveLayer = acadtextlayer
        Dim p(0 To 2) As Double
        p(0) = pt.p_x
        p(1) = pt.p_y
        p(2) = pt.p_z
        acadmodelspace.AddText(stringtext, p, h)
        Return Nothing
    End Function

    ''' <summary>
    ''' 添加线性标注
    ''' </summary>
    ''' <param name="pt1"></param>
    ''' <param name="pt2"></param>
    ''' <param name="loc"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function AddDimAligned(ByVal pt1 As Point3d, ByVal pt2 As Point3d, ByVal loc As Point3d)
        acaddoc.ActiveLayer = acadtextlayer
        Dim point1(0 To 2) As Double, point2(0 To 2) As Double
        Dim location(0 To 2) As Double
        point1(0) = pt1.p_x
        point1(1) = pt1.p_y
        point1(2) = pt1.p_z
        point2(0) = pt2.p_x
        point2(1) = pt2.p_y
        point2(2) = pt2.p_z
        location(0) = loc.p_x
        location(1) = loc.p_y
        location(2) = loc.p_z
        Dim dimalign = acadmodelspace.AddDimAligned(point1, point2, location)
        dimalign.AltUnits = False
        dimalign.PrimaryUnitsPrecision = AcDimPrecision.acDimPrecisionThree
        dimalign.ArrowheadSize = 0.2
        dimalign.TextStyle = "Mytext"
        dimalign.TextGap = 0.2
        dimalign.TextHeight = 0.2
        dimalign.HorizontalTextPosition = AcDimHorizontalJustification.acHorzCentered
        dimalign.VerticalTextPosition = AcDimVerticalJustification.acAbove
        dimalign.Fit = AcDimFit.acTextOnly
        Return (Nothing)
    End Function

    ''' <summary>
    ''' 初始化绘图空间
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function initDrawSpace()
        textstyle = acaddoc.TextStyles.Add("Mytext")
        textstyle.SetFont("Times New Roman", False, False, 0, 0)
        AddLayer("circle", AcColor.acYellow, acadcirclelayer)
        AddLayer("point", AcColor.acRed, acadpointlayer)
        AddLayer("line", AcColor.acGreen, acadlinelayer)
        AddLayer("text", AcColor.acWhite, acadtextlayer)
        acaddoc.SetVariable("PDMODE", 34)
        acaddoc.SetVariable("PDSIZE", 0.02)
        acadcirclelayer.Lineweight = ACAD_LWEIGHT.acLnWt200
        Return Nothing
    End Function

    ''' <summary>
    ''' 重新绘制当前绘图空间
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function finishDrawSpace(ByVal time As String)
        Mycad.ZoomExtents()
        acaddoc.Regen(AcRegenType.acActiveViewport)
        'acaddoc.SaveAs(Mypath + "\result\" + time + "\CAD文件\断面成果图.dwg", , )
        Return Nothing
    End Function
End Class